package com.wthink.newMain.view

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import com.wthink.hzgj.R
import com.wthink.hzgj.util.toast
import kotlinx.android.synthetic.main.dialog_change_pwd.*


class ChangePwdDialog : Dialog {
    var index = -1
    constructor(context: Context) : this(context, 0)
    constructor(context: Context, themeResId: Int) : super(context, R.style.dialog) {
        setContentView(R.layout.dialog_change_pwd)
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        quit.setOnClickListener {
            dismiss()
        }
        commit.setOnClickListener {
            val name = et_user.text.toString()
            val pwd = et_new_pwd.text.toString()
            val pwdSure = et_new_pwd_sure.text.toString()
            if (name.isEmpty() || pwd.isEmpty() || pwdSure.isEmpty()) {
                toast(getContext(),"信息不能为空")
                return@setOnClickListener
            } else  if (pwd != pwdSure){
                toast(getContext(),"两次密码输入不一致")
                return@setOnClickListener
            }
            //提交
            editInterface.editCallback(index,name,pwd)
            dismiss()

        }
    }

    fun chose(chosen: Int) {
        index = chosen
        if (index == 1) {
            et_user.setText("admin")
            et_user.inputType = EditorInfo.TYPE_NULL
        } else {
            et_user.setText("")
            et_user.inputType = EditorInfo.TYPE_CLASS_TEXT
        }
    }

    fun setOnclickListener(listener: View.OnClickListener) {
        if (commit !== null) {
            commit.setOnClickListener(listener)
        }
    }

    lateinit var editInterface: EditInterface

    fun setEditCallBack(edit: EditInterface) {
        editInterface  = edit
    }

    interface EditInterface{
        fun editCallback(index: Int,admin:String,pwd:String)
    }
}